package com.ctg.polling.domain.util.db;

import java.util.Date;
import java.util.GregorianCalendar;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.ctg.polling.domain.Choice;
import com.ctg.polling.domain.EmployeeStatus;
import com.ctg.polling.domain.JPollingUser;
import com.ctg.polling.domain.Poll;
import com.ctg.polling.domain.UserRole;
import com.ctg.polling.domain.UserSelection;

public class ContextListener implements ServletContextListener {

	/**
	 * Initialize DB (only for dev environnement.
	 */
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		// TODO : write a flag to activate this code only in dev environment.
		if (JPollingUser.countJPollingUsers() == 0  && Poll.countPolls() == 0 && UserSelection.countUserSelections() == 0) {
			initDB();
		}
	}

	private void initDB() {
		// if db is empty, then initialize it with testing data.
		if (JPollingUser.countJPollingUsers() == 0) {
			JPollingUser author = new JPollingUser();
			author.setUsername("jb");
			author.setPassword("password");
			author.setUserRole(UserRole.ROLE_USER);
			author.setOpenIdIdentifier("https://www.google.com/accounts/o8/id?id=AItOawlDWfx5RswyhMd7wg544l_R1odWiVfKDxU");
			author.setStatus(EmployeeStatus.ACTIVE);
			author.setEmail("jb.renaux@gmail.com");
			author.persist();
				
			Poll poll = new Poll();
			poll.setMultipleChoice(true);
			poll.setQuestion("test_question");
			poll.setExpireDate(new Date());
			poll.setAuthor(author);
			poll.persist();
			
			Poll poll2 = new Poll();
			poll2.setMultipleChoice(false);
			poll2.setQuestion("test_question2");
			GregorianCalendar gregorianCalendar = new GregorianCalendar();
			gregorianCalendar.set(2100, 10, 25);
			poll2.setExpireDate(gregorianCalendar.getTime());
			poll2.setAuthor(author);
			poll2.persist();
			
			Choice choice1 = new Choice();
			choice1.setLabel("choice 1");
			choice1.setPoll(poll);
			choice1.persist();
			
			UserSelection selection = new UserSelection();
			selection.setPolled(author);
			selection.setChoice(choice1);
			selection.setSelectionDate(new Date());
			selection.persist();
			
			Choice choice2 = new Choice();
			choice2.setLabel("choice 2");
			choice2.setPoll(poll);
			choice2.persist();
			
			Choice choice3 = new Choice();
			choice3.setLabel("choice 3");
			choice3.setPoll(poll2);
			choice3.persist();
	
			UserSelection selection2 = new UserSelection();
			selection2.setPolled(author);
			selection2.setChoice(choice3);
			selection2.setSelectionDate(new Date());
			selection2.persist();
			
			Choice choice4 = new Choice();
			choice4.setLabel("choice 4");
			choice4.setPoll(poll2);
			choice4.persist();
			
			Poll poll3 = new Poll();
			poll3.setMultipleChoice(true);
			poll3.setQuestion("test_question 3");
			poll3.setExpireDate(new Date());
			poll3.setAuthor(author);
			poll3.persist();
			
			Choice choice5 = new Choice();
			choice5.setLabel("choice 5");
			choice5.setPoll(poll3);
			choice5.persist();

			Choice choice6 = new Choice();
			choice6.setLabel("choice 6");
			choice6.setPoll(poll3);
			choice6.persist();

			JPollingUser author2 = new JPollingUser();
			author2.setUsername("admin");
			author2.setPassword("admin");
			author2.setUserRole(UserRole.ROLE_ADMIN);
			author2.setStatus(EmployeeStatus.ACTIVE);
			author2.setEmail("jb.renaux@gmail.com");
			author2.persist();
		}
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub

	}

}
